/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.metadata.staticschemas;

import java.io.InputStream;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;

public class FedoraRelsExtOntology {
	 /** <p>The RDF model that holds the vocabulary terms</p> */
    private static Model m_model = ModelFactory.createDefaultModel();
    
    /** <p>The namespace of the vocabulary as a string</p> */
    public static final String NS = "info:fedora/fedora-system:def/relations-external#";
    
    /** <p>The namespace of the vocabulary as a string</p>
     *  @see #NS */
    public static String getURI() {return NS;}
    
    /** <p>The namespace of the vocabulary as a resource</p> */
    public static final Resource NAMESPACE = m_model.createResource( NS );
    
    /** <p>A refinement of the generic descriptive relationship indicating a commentary 
     *  relationship between fedora objects. The subject is a fedora object that is 
     *  being commented on and the predicate is a fedora object that represents an 
     *  annotation or comment about the subject.</p>
     */
    public static final Property HAS_ANNOTATION = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#HasAnnotation" );
    
    /** <p>A generic descriptive relationship between fedora objects. The subject is 
     *  a fedora object that is being described in some manner and the predicate is 
     *  a fedora object that represents a descriptive entity that is about the subject.</p>
     */
    public static final Property HAS_DESCRIPTION = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#HasDescription" );
    
    /** <p>A refinement of the generic descriptive relationship indicating a metadata 
     *  relationship between fedora objects. The subject is a fedora object and the 
     *  predicate is a fedora object that represents metadata about the subject.</p>
     */
    public static final Property HAS_METADATA = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#HasMetadata" );
    
    /** <p>The primitive property for all object-to-object relationships in the fedora 
     *  ontology</p>
     */
    public static final Property FEDORA_RELATIONSHIP = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#fedoraRelationship" );
    
    /** <p>This is a refinement of the generic membership to indicate the notion of a 
     *  digital collection. The subject is a fedora object representing a whole digital 
     *  collection and the predicate is a fedora object representing an item that 
     *  is a member of the digital collection.</p>
     */
    public static final Property HAS_COLLECTION_MEMBER = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#hasCollectionMember" );
    
    /** <p>This is a refinement of the generic part/whole relationship in the "made of" 
     *  sense with referential integrity implications. The subject is a fedora object 
     *  representing a whole that is made of constituent parts and the predicate is 
     *  a fedora object representing one of the constituent parts. The whole loses 
     *  its integrity if the constituent part is removed.</p>
     */
    public static final Property HAS_CONSTITUENT = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#hasConstituent" );
    
    /** <p>A definition of a generic dependency relationship between fedora objects. 
     *  The subject is a fedora object that is depended upon and the predicate is 
     *  a fedora object that represents a dependent of the subject.</p>
     */
    public static final Property HAS_DEPENDENT = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#hasDependent" );
    
    /** <p>A definition of a generic derivation relationship between fedora objects. 
     *  The subject is a fedora object and the predicate is a fedora object that represents 
     *  a derivation of the subject.</p>
     */
    public static final Property HAS_DERIVATION = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#hasDerivation" );
    
    /** <p>A definition of a generic equivalence relationship between fedora objects. 
     *  The subject is a fedora object that is equivalent to the predicate which is 
     *  another fedora objects.</p>
     */
    public static final Property HAS_EQUIVALENT = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#hasEquivalent" );
    
    /** <p>This is a refinement of the generic part/whole relationship that defines a 
     *  set membership relationship between fedora objects. The subject is a fedora 
     *  object representing a whole set and the predicate is a fedora object representing 
     *  a member in the set. The set does not lose its integrity if a member is removed 
     *  from the set.</p>
     */
    public static final Property HAS_MEMBER = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#hasMember" );
    
    /** <p>A definition of the generic part/whole relationship between fedora objects. 
     *  The subject is a fedora object representing a whole and the predicate is a 
     *  fedora object representing a part.</p>
     */
    public static final Property HAS_PART = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#hasPart" );
    
    /** <p>This is a refinement of the generic set membership to indicate the notion 
     *  of a subset. The subject is a fedora object that represents a set of objects 
     *  and the predicate is a fedora object that represents a subset of the subject 
     *  set.</p>
     */
    public static final Property HAS_SUBSET = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#hasSubset" );
    
    /** <p>A refinement of the generic descriptive relationship indicating a commentary 
     *  relationship between fedora objects. The subject is a fedora object that represents 
     *  an annotation or comment and the predicate is a fedora object that is being 
     *  commented upon by the subject.</p>
     */
    public static final Property IS_ANNOTATION_OF = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#isAnnotationOf" );
    
    /** <p>This is a refinement of the generic part/whole relationship in the "made of" 
     *  sense with referential integrity implications. The subject is a fedora object 
     *  representing a constituent part and the predicate is a fedora object representing 
     *  a whole of which the subject is a constituent. The constituent part cannot 
     *  stand alone in any meaningful way without reference to the whole.</p>
     */
    public static final Property IS_CONSTITUENT_OF = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#isConstituentOf" );
    
    /** <p>A definition of a generic dependency relationship between fedora objects. 
     *  The subject is a fedora object that represents a dependent and the predicate 
     *  is another fedora object that is depended upon by the subject.</p>
     */
    public static final Property IS_DEPENDENT_OF = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#isDependentOf" );
    
    /** <p>A definition of a generic derivation relationship between fedora objects. 
     *  The subject is a fedora object that represents a derivation of the predicate 
     *  which is another fedora object.</p>
     */
    public static final Property IS_DERIVATION_OF = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#isDerivationOf" );
    
    /** <p>A generic descriptive relationship between fedora objects. The subject is 
     *  a fedora object that represents a descriptive entity and the predicate is 
     *  a fedora object that is being described in some manner by the subject.</p>
     */
    public static final Property IS_DESCRIPTION_OF = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#isDescriptionOf" );
    
    /** <p>This is a refinement of the generic part/whole relationship that defines a 
     *  set membership relationship between fedora objects. The subject is a fedora 
     *  object representing a member of a set and the predicate is a fedora object 
     *  representing a whole set of which the subject is a member. The member can 
     *  be separated from the set and still stand alone as an object in its own right.</p>
     */
    public static final Property IS_MEMBER_OF = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#isMemberOf" );
    
    /** <p>This is a refinement of the generic membership relationship to indicate the 
     *  notion of a digital collection. The subject is a fedora object representing 
     *  an item in a digital collection and the predicate is a fedora object representing 
     *  a whole digital collection.</p>
     */
    public static final Property IS_MEMBER_OF_COLLECTION = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#isMemberOfCollection" );
    
    /** <p>A refinement of the generic descriptive relationship indicating a metadata 
     *  relationship between fedora objects. The subject is a fedora object that represents 
     *  metadata and the predicate is a fedora object for which the subject serves 
     *  as metadata.</p>
     */
    public static final Property IS_METADATA_FOR = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#isMetadataFor" );
    
    /** <p>A definition of the generic part/whole relationship between fedora objects. 
     *  The subject is a fedora object representing a part and the predicate is a 
     *  fedora object representing a whole.</p>
     */
    public static final Property IS_PART_OF = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#isPartOf" );
    
    /** <p>This is a refinement of the generic set membership relationship to indicate 
     *  the notion of a subset. The subject is a fedora object that represents a subset 
     *  and the predicate is a fedora object that represents the set of which the 
     *  subject is a subset.</p>
     */
    public static final Property IS_SUBSET_OF = m_model.createProperty( "info:fedora/fedora-system:def/relations-external#isSubsetOf" );
    
  /*  public static Model getModel()
    {
    	Model myModel = ModelFactory.createDefaultModel();
    	myModel.createResource( NS );
    	myModel.createProperty( NS, "isPartOf" );
    	myModel.createProperty( NS, "isSubsetOf" );
    	myModel.createProperty( NS, "isMetadataFor" );
    	myModel.createProperty( NS, "isMemberOfCollection" );
    	myModel.createProperty( NS, "isMemberOf" );
    	myModel.createProperty( NS, "isDescriptionOf" );
    	myModel.createProperty( NS, "isDerivationOf" );
    	myModel.createProperty( NS, "isDependentOf" );
    	myModel.createProperty( NS, "isConstituentOf" );
    	myModel.createProperty( NS, "isAnnotationOf" );
    	myModel.createProperty( NS, "hasSubset" );
    	myModel.createProperty( NS, "hasPart" );
    	myModel.createProperty( NS, "hasMember" );
    	myModel.createProperty( NS, "hasEquivalent" );
    	myModel.createProperty( NS, "hasDerivation" );
    	myModel.createProperty( NS, "hasDependent" );
    	myModel.createProperty( NS, "hasConstituent" );
    	myModel.createProperty( NS, "hasDerivation" );
    	myModel.createProperty( NS, "hasCollectionMember" );
    	myModel.createProperty( NS, "fedoraRelationship" );
    	myModel.createProperty( NS, "HasMetadata" );
    	myModel.createProperty( NS, "HasDescription" );
    	myModel.createProperty( NS, "HasAnnotation" );
    	
    	return myModel;
    }*/
    
    public static Model getModel() {
	ClassLoader classLoader = Thread.currentThread()
	.getContextClassLoader();
	InputStream is;
	is = classLoader.getResourceAsStream("fedora-rels.rdf");

    	Model myModel = ModelFactory.createDefaultModel();
    	
    	try {
    	    myModel.read(is, "");
    	    return myModel;
    	}catch (Exception ex) {
    	    return myModel;
    	}      
    }
    public static boolean containsProperty(String elementName)
    {
    	if (elementName.contains(":"))
    		elementName = elementName.substring(elementName.indexOf(":") + 1);
    	
    	if (!FedoraRelsExtOntology.HAS_ANNOTATION.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.HAS_COLLECTION_MEMBER.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.HAS_CONSTITUENT.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.HAS_DEPENDENT.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.HAS_DERIVATION.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.HAS_DESCRIPTION.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.HAS_EQUIVALENT.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.HAS_MEMBER.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.HAS_METADATA.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.HAS_PART.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.HAS_PART.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.HAS_SUBSET.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.IS_ANNOTATION_OF.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.IS_CONSTITUENT_OF.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.IS_DEPENDENT_OF.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.IS_DERIVATION_OF.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.IS_DESCRIPTION_OF.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.IS_MEMBER_OF.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.IS_MEMBER_OF_COLLECTION.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.IS_METADATA_FOR.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.IS_PART_OF.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.IS_PART_OF.getLocalName().equals(elementName) &&
    			!FedoraRelsExtOntology.IS_SUBSET_OF.getLocalName().equals(elementName))
    			return false;
    		else return true;
    }
}
